﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.SysSetting
{
    public partial class uc_Sys_Settings_Tree_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public LeverString_SysTreeMainList _tl = new LeverString_SysTreeMainList();
        public string _LoadType = "";
        public uc_Sys_Settings_Tree_ManaForm()
        {
            InitializeComponent();
        }

        private void uc_Sys_Settings_Tree_ManaForm_Load(object sender, EventArgs e)
        {
            //_tl = (LeverString_SysTreeMainList)this.Tag;
            te_Name.Text = _tl.f_name;
            te_Str_0.Text = _tl.f_str_0;
            te_Str_1.Text = _tl.f_str_1;
            te_Str_2.Text = _tl.f_str_2;
            te_Func_0.Text = _tl.f_func_0;
            te_Func_1.Text = _tl.f_func_1;
            te_Func_2.Text = _tl.f_func_2;
            if (_tl.f_type == "0")
            {
                rb_1.Checked = false;
                rb_0.Checked = true;
            }
            else
            {
                rb_0.Checked = false;
                rb_1.Checked = true;
            }
            if (_LoadType == "Add")
            {
                bt_Sure.Text = "新  增";
            }
            else if (_LoadType == "Edit")
            {
                bt_Sure.Text = "修  改";
            }
            else
            {
                bt_Sure.Text = "删  除";
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string f_type="0";
            if (rb_1.Checked)
            {
                f_type="1";
            }
            if (_LoadType == "Add")
            {
                if (_tl.f_id.Substring(0, 1) == "M")
                {
                    strSQL = "insert into TS_S_T_P_Tree_Main (f_name,f_level,f_deep,f_type,f_str_0,f_str_1,f_str_2,f_func_0,f_func_1,f_func_2) values ('" + te_Name.Text.Trim() + "',',',0," + f_type + ",'" + te_Str_0.Text.Trim() + "','" + te_Str_1.Text.Trim() + "','" + te_Str_2.Text.Trim() + "','" + te_Func_0.Text.Trim() + "','" + te_Func_1.Text.Trim() + "','" + te_Func_2.Text.Trim() + "')";
                }
                else
                {
                    strSQL = "insert into TS_S_T_P_Tree_Main (f_name,f_level,f_deep,f_type,f_str_0,f_str_1,f_str_2,f_func_0,f_func_1,f_func_2) values ('" + te_Name.Text.Trim() + "','"+_tl.f_level+_tl.f_id.Substring(1)+",',"+(Convert.ToInt32(_tl.f_deep)+1).ToString()+"," + f_type + ",'" + te_Str_0.Text.Trim() + "','" + te_Str_1.Text.Trim() + "','" + te_Str_2.Text.Trim() + "','" + te_Func_0.Text.Trim() + "','" + te_Func_1.Text.Trim() + "','" + te_Func_2.Text.Trim() + "')";
                }
            }
            else if (_LoadType == "Edit")
            {
                strSQL = "update TS_S_T_P_Tree_Main set f_name='" + te_Name.Text.Trim() + "',f_type=" + f_type + ",f_str_0='" + te_Str_0.Text.Trim() + "',f_str_1='" + te_Str_1.Text.Trim() + "',f_str_2='" + te_Str_2.Text.Trim() + "',f_func_0='" + te_Func_0.Text.Trim() + "',f_func_1='" + te_Func_1.Text.Trim() + "',f_func_2='" + te_Func_2.Text.Trim() + "',f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id=" + _tl.f_id.Substring(1);
            }
            else
            {
                //删除当前目录
                strSQL = "update TS_S_T_P_Tree_Main set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _tl.f_id.Substring(1);
                //删除子目录
                strSQL += ";update TS_S_T_P_Tree_Main set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_level like '%," + _tl.f_id.Substring(1) + ",%' and f_del=0";
                //删除目录及子目录的目录数据表
                strSQL += ";update TS_S_T_P_Tree set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ") and f_del=0";
                //删除目录及子目录的目录资源表
                strSQL += ";update TS_S_T_P_Tree_Resource set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_mainid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除目录及子目录的目录关联表
                strSQL += ";update TS_S_T_P_Tree_Relation set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_tlid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除目录及子目录的目录授权表
                strSQL += ";update TS_S_T_S_Tree_Authority set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_menuid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除报表字段
                strSQL += ";update TS_S_T_P_Report_Field set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_rmid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除报表关联流程
                strSQL += ";update TS_S_T_P_Report_Tech set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_treeid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除报表关联统计字段
                strSQL += ";update TS_S_T_P_Report_Field_WP set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_rmid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除报表关联填报部门
                strSQL += ";update TS_S_T_P_Report_Dep set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_rmid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + ")) and f_del=0";
                //删除报表字段填报授权
                strSQL += ";update TS_S_T_P_Report_Authority set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_filedid in(select f_id from TS_S_T_P_Report_Field where f_rmid in( select f_id from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_Main where f_level like '%," + _tl.f_id.Substring(1) + ",%' or f_id=" + _tl.f_id.Substring(1) + "))) and f_del=0";
            }
            TS_Win_Class.MySqlCom(strSQL);
            this.DialogResult = DialogResult.OK;
        }

        private void te_Name_EditValueChanged(object sender, EventArgs e)
        {
            if (te_Name.Text.Trim().Length > 0 && te_Str_0.Text.Trim().Length > 0 && te_Str_1.Text.Trim().Length > 0 && te_Str_2.Text.Trim().Length > 0 && te_Func_0.Text.Trim().Length > 0 && te_Func_1.Text.Trim().Length > 0 && te_Func_2.Text.Trim().Length > 0)
            {
                bt_Sure.Enabled = true;
            }
            else
            {
                bt_Sure.Enabled = false;
            }
        }
    }
}